package eng.jTrussSolver.GUI.auxilliary;

import java.awt.Choice;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import eng.jTrussSolver.Force.ForceData;
import eng.jTrussSolver.Solver.truss2dProblemDef;


public class SecurityConditions {
	
	public boolean checkMaterialFieldsIfNull(JTextField tFNameOfMaterial, JTextField tFEOfMaterial, JTextField tFs_epOfMaterial) {
		boolean allow = true;
		String NameFields = tFNameOfMaterial.getText();
		String ModulusField = tFEOfMaterial.getText();
		String sepFields = tFs_epOfMaterial.getText();
		
		if (NameFields.equals("") || ModulusField.equals("") || sepFields.equals("")) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: All fields must be completed.", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		
		return allow;
	}
	
	public boolean checkMaterialNameIfExists(truss2dProblemDef tr2d, String OldMaterialName, JTextField tFNameOfMaterial) {
		boolean allow = true;
		if (OldMaterialName.equals(tFNameOfMaterial.getText())) {
			allow = true;
		}
		else {
			for (String MaterialName : tr2d.getMaterials().keySet()) {
				 if (MaterialName.equals(tFNameOfMaterial.getText())){
				       JOptionPane.showMessageDialog(null,
				          "Error: This name of Material already exists.Please enter a different name for this Material!", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				       allow = false;
				       break;
				  }
			}
		}
		return allow;
	}
	
	public boolean checkNewMaterialNameIfExists(truss2dProblemDef tr2d, JTextField tFNameOfMaterial) {
		boolean allow = true;
		for (String MaterialName : tr2d.getMaterials().keySet()) {
			 if (MaterialName.equals(tFNameOfMaterial.getText())){
			       JOptionPane.showMessageDialog(null,
			          "Error: This name of Material already exists.Please enter a different name for this Material!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
			       allow = false;
			       break;
			  }
		}
		return allow;
	}
	
	public boolean checkMaterialFieldsIfAccepted(JTextField tFEOfMaterial, JTextField tFs_epOfMaterial) {
		boolean allow = true;
		String ModulusField = tFEOfMaterial.getText();
		String sepFields = tFs_epOfMaterial.getText();
		
		try {
			Double.parseDouble(ModulusField);
		}
		catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null,
			          "Error: The value Modulus of elasticity must be countable!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
			allow = false;
		}
		// H if prostethike gia na emfanizetai ena parathuro tin fora (stin periptosi poy einai kai oi 2 times den einai arithmoi).
		if (allow == true) {
			try {
				Double.parseDouble(sepFields);
			}
			catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null,
				          "Error: The value of Allowable stress must be countable!", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				allow = false;
			}
		}
		return allow;
	}
	
	public boolean checkSectionFieldsIfNull(JTextField tFNameOfSection, JTextField tFAreaOfSection, JTextField tFIxxOfSection) {
		boolean allow = true;
		
		if (tFNameOfSection.getText().equals("") || tFAreaOfSection.getText().equals("") || tFIxxOfSection.getText().equals("")) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: All fields must be completed.", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		
		return allow;
	}
	
	public boolean checkSectionNameIfExists(truss2dProblemDef tr2d, String OldSectionName, JTextField tFNameOfSection) {
		boolean allow = true;
		if (tFNameOfSection.getText().equals(OldSectionName)) {
			allow = true;
		}
		else {
			for (String SectionName : tr2d.getSections().keySet()) {
				 if (SectionName.equals(tFNameOfSection.getText())){
				       JOptionPane.showMessageDialog(null,
				          "Error: This name of Section already exists.Please enter a different name for this Section!", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				       allow = false;
				       break;
				  }
			}
		}
		return allow;
	}
	
	public boolean checkNewSectionNameIfExists(truss2dProblemDef tr2d,
			JTextField tFNameOfSection) {
		boolean allow = true;
		for (String SectionName : tr2d.getSections().keySet()) {
			 if (SectionName.equals(tFNameOfSection.getText())){
			       JOptionPane.showMessageDialog(null,
			          "Error: This name of Section already exists.Please enter a different name for this Section!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
			       allow = false;
			       break;
			  }
		}
		return allow;
	}
	
	public boolean checkSectionFieldsIfAccepted(JTextField tFAreaOfSection, JTextField tFIxxOfSection) {
		boolean allow = true;
		
		try {
			Double.parseDouble(tFAreaOfSection.getText());
		}
		catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null,
			          "Error: The value of Area must be countable!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
			allow = false;
		}
		
		if (allow == true) {
			try {
				Double.parseDouble(tFIxxOfSection.getText());
			}
			catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null,
				          "Error: The value of Moment of Inertia must be countable!", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				allow = false;
			}
		}
		return allow;
	}
	
	public boolean checkNodeFieldsIfNull(JTextField tFxNode, JTextField tFyNode) {
		boolean allow = true;

		if (tFxNode.getText().equals("") || tFyNode.getText().equals("")) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: All fields must be completed.", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		
		return allow;
	}

	public boolean checkNodeIfAlreadyExists(truss2dProblemDef tr2d, JTextField tFxNode,
			JTextField tFyNode) {
		boolean allow = true;
		double X = Double.parseDouble(tFxNode.getText());
		double Y = Double.parseDouble(tFyNode.getText());
		for (Integer NodeKey : tr2d.getNodes().keySet()) {
			if ((tr2d.getNodes().get(NodeKey).X == X) && (tr2d.getNodes().get(NodeKey).Y == Y)) {
				allow = false;
				JOptionPane.showMessageDialog(null,
				          "Error: This Node already exists.", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				break;
			}
		}
		return allow;
	}

	public boolean checkNodeFieldsIfAccepted(JTextField tFxNode,
			JTextField tFyNode) {
		boolean allow = true;
		try {
			Double.parseDouble(tFxNode.getText());
		}
		catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null,
			          "Error: The value of X must be countable!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
			allow = false;
		}
		
		if (allow == true) {
			try {
				Double.parseDouble(tFyNode.getText());
			}
			catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null,
				          "Error: The value of Y must be countable!", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				allow = false;
			}
		}
		return allow;
	}
	
	public boolean checkUpdatedNodeIfAlreadyExists(truss2dProblemDef tr2d,
			JTextField tFxNode, JTextField tFyNode, JList<String> lstOfNodes) {
		boolean allow = true;
		int UpdatedNode = tr2d.getNodes().getStringElementID(lstOfNodes.getSelectedValue());
		double UpdatedNodeX = Double.parseDouble(tFxNode.getText());
		double UpdatedNodeY = Double.parseDouble(tFyNode.getText());
		for (Integer key : tr2d.getNodes().keySet()) {
			if (tr2d.getNodes().get(key).NodeID != UpdatedNode) {
				if (tr2d.getNodes().get(key).X == UpdatedNodeX &&
						tr2d.getNodes().get(key).Y == UpdatedNodeY) {
					allow = false;
					JOptionPane.showMessageDialog(null,
					          "Error: This Node already exists.", "Error Message",
					          JOptionPane.ERROR_MESSAGE);
					break;
				}
					
			}
		}
		return allow;
	}
	
	public boolean checkRodFieldsAndChoiceIfNull(truss2dProblemDef tr2d, JTextField tFNameOfRod, Choice chcMaterial,
			Choice chcSection, Choice chcNodeStart, Choice chcNodeEnd) {	
		boolean allow = true;
		if (tFNameOfRod.getText().equals("")) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: Please enter a name for this Rod.", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		if (allow != false && tr2d.getNodes().getNodesCount() == 0) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: The list with Nodes is blank!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		if (allow != false && tr2d.getSections().getSectionsCount() == 0) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: The list with Sections is blank!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		if (allow != false && tr2d.getMaterials().getMaterialsCount() == 0) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: The list with Materials is blank!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		return allow;
	}
	
	public boolean checkNewRodNameIfExists(truss2dProblemDef tr2d, JTextField tFNameOfNewRod) {
		boolean allow = true;
		for (Integer RodID : tr2d.getRods().keySet()) {
			String RodName = tr2d.getRods().get(RodID).Name;
			 if (RodName.equals(tFNameOfNewRod.getText())){
			       JOptionPane.showMessageDialog(null,
			          "Error: This Rod's name already exists.It's recommended to enter a different name for this Rod!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
			       allow = false;
			       break;
			  }
		}
		return allow;
	}

	public boolean checkRodSelectedNodesIfEquals(Choice chcNodeStart,
			Choice chcNodeEnd) {
		boolean allow = true;
		if (chcNodeStart.getSelectedItem().equals(chcNodeEnd.getSelectedItem())) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: The selected start and end Node must not be the same!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		return allow;
	}

	public boolean checkSelectedNodesIfAlreadyConnected(truss2dProblemDef tr2d,
			Choice chcNodeStart, Choice chcNodeEnd) {
		boolean allow = true;
		int StartNode = tr2d.getNodes().getStringElementID(chcNodeStart.getSelectedItem());
		int EndNode = tr2d.getNodes().getStringElementID(chcNodeEnd.getSelectedItem());
		for (Integer key : tr2d.getRods().keySet()) {
			int tmpRodStart = tr2d.getRods().get(key).NodeStart;
			int tmpRodEnd = tr2d.getRods().get(key).NodeEnd;
			if ((tmpRodStart == StartNode && tmpRodEnd == EndNode) || (tmpRodStart == EndNode && tmpRodEnd == StartNode)) {
				allow = false;
				JOptionPane.showMessageDialog(null,
				          "Error: The selected Nodes are already connected with a Rod", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				break;
			}
		}
		return allow;
	}

	public boolean checkRodNewNameIfAlreadyExists(truss2dProblemDef tr2d,
			String oldRodName, JTextField tFNameOfRod) {
		boolean allow = true;
		if (oldRodName.equals(tFNameOfRod.getText())) {
			allow = true;
		}
		else {
			for (Integer RodID : tr2d.getRods().keySet()) {
				String RodName = tr2d.getRods().get(RodID).Name;
				 if (RodName.equals(tFNameOfRod.getText())){
				       JOptionPane.showMessageDialog(null,
				          "Error: This Rod's name already exists.It's recommended to enter a different name for this Rod!", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				       allow = false;
				       break;
				  }
			}
			
		}
		return allow;
	}

	public boolean checkRodNewNodesIfAlreadyConnected(truss2dProblemDef tr2d,
			int rodID, Choice chcNodeStart, Choice chcNodeEnd) {
		boolean allow = true;
		int StartNodeID = tr2d.getNodes().getStringElementID(chcNodeStart.getSelectedItem());
		int EndNodeID = tr2d.getNodes().getStringElementID(chcNodeEnd.getSelectedItem());
		if (StartNodeID == tr2d.getRods().get(rodID).NodeStart && EndNodeID == tr2d.getRods().get(rodID).NodeEnd
				|| StartNodeID == tr2d.getRods().get(rodID).NodeEnd && EndNodeID == tr2d.getRods().get(rodID).NodeStart) {
			allow = true;
		}
		else {
			for (Integer RodID : tr2d.getRods().keySet()) {
				int tmpNodeStart = tr2d.getRods().get(RodID).NodeStart;
				int tmpNodeEnd = tr2d.getRods().get(RodID).NodeEnd;
				 if (tmpNodeStart == StartNodeID && tmpNodeEnd == EndNodeID ||
						 tmpNodeStart == EndNodeID && tmpNodeEnd == StartNodeID){
				       JOptionPane.showMessageDialog(null,
				          "Error: The selected Nodes are already connected with a Rod", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				       allow = false;
				       break;
				  }
			}
		}
		
		return allow;
	}

	
	public boolean checkForceFieldsIfNull(JTextField tFForceValue,
			JTextField tFForceAngle) {
		boolean allow = true;
		if (tFForceValue.getText().equals("") || tFForceAngle.getText().equals("")) {
			allow = false;
			JOptionPane.showMessageDialog(null,
			          "Error: All fields must be completed.", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
		}
		return allow;
	}

	public boolean checkForceFieldsIfNumbers(JTextField tFForceValue,
			JTextField tFForceAngle) {
		boolean allow = true;
		try {
			 Double.parseDouble(tFForceValue.getText());
		}
		catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null,
			          "Error: The Force value must be countable!", "Error Message",
			          JOptionPane.ERROR_MESSAGE);
			allow = false;
		}
		if (allow != false) {
			try {
				Double.parseDouble(tFForceAngle.getText());
			}
			catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null,
				          "Error: The angle of Force must be countable!", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				allow = false;
			}
		}
		
		return allow;
	}

	public boolean CheckForceIfAlreadyExists(truss2dProblemDef tr2d,
			JTextField tFForceValue, JTextField tFForceAngle, JList<String> lstOfNode_Forces) {
		boolean allow = true;
		double ForceValue = Double.parseDouble(tFForceValue.getText());
		double ForceAngle = Double.parseDouble(tFForceAngle.getText());
		int NodeIDofForce = tr2d.getNodes().getStringElementID(lstOfNode_Forces.getSelectedValue());
		for (Integer key : tr2d.getForces().keySet()) {
			if (tr2d.getForces().get(key).getMagnitude() == ForceValue &&
					tr2d.getForces().get(key).getAngle_Degrees() == ForceAngle
					&& NodeIDofForce == tr2d.getForces().get(key).getNodeIDofForce()) {
				int answer = JOptionPane.showConfirmDialog(null,
				          "Error: This Force already exists.Do you want to sum?", "Error Message",
				          JOptionPane.OK_CANCEL_OPTION);
				if (answer == 2) {
					allow = false;
					break;
				}
				if (answer == 0) {
					allow = false;
					ForceData EditForce = tr2d.getForces().get(key);
					double newForceValue = EditForce.getMagnitude() + ForceValue;
					ForceData newForce = new ForceData(key, NodeIDofForce, newForceValue, ForceAngle);
					tr2d.getForces().put(key, newForce);
					break;
				}
			}
		}
		return allow;
	}
	
	public boolean CheckUpdatedForceIfAlreadyExists(truss2dProblemDef tr2d,
			JTextField tFForceValue, JTextField tFForceAngle, JList<String> lstOfNode_Forces,
			JList<String> lstOfForces) {
		boolean allow = true;
		double ForceValue = Double.parseDouble(tFForceValue.getText());
		double ForceAngle = Double.parseDouble(tFForceAngle.getText());
		int NodeIDofForce = tr2d.getNodes().getStringElementID(lstOfNode_Forces.getSelectedValue());
		int ForceID = tr2d.getForces().getStringElementID(lstOfForces.getSelectedValue());
		if (tr2d.getForces().get(ForceID).getNodeIDofForce() != NodeIDofForce) {
			for (Integer key : tr2d.getForces().keySet()) {
				if (key != ForceID) {
					if (tr2d.getForces().get(key).getMagnitude() == ForceValue &&
							tr2d.getForces().get(key).getAngle_Degrees() == ForceAngle
							&& NodeIDofForce == tr2d.getForces().get(key).getNodeIDofForce()) {
						int answer = JOptionPane.showConfirmDialog(null,
						          "Error: This Force already exists.Do you want to sum?", "Error Message",
						          JOptionPane.OK_CANCEL_OPTION);
						if (answer == 2) {
							allow = false;
							break;
						}
						if (answer == 0) {
							allow = false;
							ForceData EditForce = tr2d.getForces().get(key);
							double newForceValue = EditForce.getMagnitude() + ForceValue;
							ForceData newForce = new ForceData(key, NodeIDofForce, newForceValue, ForceAngle);
							tr2d.getForces().put(key, newForce);
							tr2d.getForces().removeForce(ForceID);
							break;
						}
					}
				}
			}
		}
		else {
			allow = true;
		}
		return allow;
	}
	
	/**
	 * This method check for connected rods to a node that is being deleted.
	 * @param tr2d
	 * @param NodeKey: Checking Node
	 * @return true if someone rod or rods is connected to that node. If some rod isn't connected to node, then return false.
	 */
	public boolean checkIfConnectedRodsOnNode(truss2dProblemDef tr2d, int NodeKey) {
		
		boolean dontDelete = false;
		for (Integer key : tr2d.getRods().keySet()) {
			int tmpStartNode = tr2d.getRods().get(key).NodeStart;
			int tmpEndNode = tr2d.getRods().get(key).NodeEnd;
			String tmpRodName = tr2d.getRods().get(key).Name;
			if ((tmpStartNode == NodeKey) || (tmpEndNode == NodeKey)) {
				JOptionPane.showMessageDialog(null,
				          "Error: This rod (" + tmpRodName + ") is connected to this node." + "\n"
				          + "Please delete rod and try again.", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				dontDelete = true;
				break;
			}		
		}
		return dontDelete;
	}

	/**
	 * This method checks if forces are applied to the node.
	 * @param tr2d
	 * @param nodeKey
	 * @return true if someone force is applied to the node. False if not.
	 */
	public boolean checkIfActionForcesOnNode(truss2dProblemDef tr2d, int nodeKey) {
		boolean dontDelete = false;
		for (Integer key : tr2d.getForces().keySet()) {
			int tmpNodeIDofForce = tr2d.getForces().get(key).getNodeIDofForce();
			if (tmpNodeIDofForce == nodeKey) {
			
				JOptionPane.showMessageDialog(null,
				          "Error: Some forces are applied on this Node. \n" + 
				        		  "Please delete these forces and try again.", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				dontDelete = true;
				break;
			}
		}
		return dontDelete;
	}

	/**
	 * This method check if material is registered to a Rod before delete it.
	 * @param tr2d
	 * @param jListElement: Name of Material to be deleted.
	 * @return true if material is registered to a Rod. False if not.
	 */
	public boolean checkIfSectionIsRegToRod(truss2dProblemDef tr2d,
			String jListElement) {
		boolean dontDelete = false;
		for (Integer key : tr2d.getRods().keySet()) {
			String tmpSectionName = tr2d.getRods().get(key).RodSection;
			String tmpRodName = tr2d.getRods().get(key).Name;
			if (jListElement.equals(tmpSectionName)) {
				JOptionPane.showMessageDialog(null,
				          "Error: This Section is registered to a Rod with name (" + 
				        		 tmpRodName + "). \n" + "Please delete or change this Rod and try again.", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				dontDelete = true;
				break;
			}
		}
		return dontDelete;
	}

	/**
	 * This method check if material to be deleted is registered to a Rod.
	 * @param tr2d
	 * @param _MaterialName: Material to be checked.
	 * @return true if Material is registered and false if not registered. If true, then can't deleted.
	 */
	public boolean checkIfMaterialIsRegToRod(truss2dProblemDef tr2d,
			String _MaterialName) {
		boolean dontDelete = false;
		for (Integer key : tr2d.getRods().keySet()) {
			String tmpMaterialName = tr2d.getRods().get(key).RodMaterial;
			String tmpRodName = tr2d.getRods().get(key).Name;
			if (_MaterialName.equals(tmpMaterialName)) {
				JOptionPane.showMessageDialog(null,
				          "Error: This Material is registered to a Rod with name (" + 
				        		 tmpRodName + "). \n" + "Please delete or change this Rod and try again.", "Error Message",
				          JOptionPane.ERROR_MESSAGE);
				dontDelete = true;
				break;
			}
		}
		return dontDelete;
	}
}
	
